package com.ezreal.ezchat.utils.permission;

import com.ezreal.ezchat.bean.LocalAccountBean;
import com.ezreal.ezchat.handler.NimUserHandler;
import com.suntek.commonlibrary.utils.TextUtils;

public class PermissionManager {

    private LocalAccountBean localAccountBean;

    private static final class SingleHolder {
        private static final PermissionManager INSTANCE = new PermissionManager();
    }

    public static PermissionManager getInstance() {
        return SingleHolder.INSTANCE;
    }

    private PermissionManager() {
        NimUserHandler.getInstance().init();
    }

    /**
     * 检查消息密码是否打开
     */
    public boolean isPermissionOpen() {
        return !TextUtils.isEmpty(getMsgPwd());
    }

    /**
     * 检查消息密码是否正确
     */
    public boolean checkPwdSuccess(String pwd) {
        if (TextUtils.isEmpty(pwd)) {
            return false;
        }
        String md = TextUtils.getMD5(pwd);
        return TextUtils.equals(md, getMsgPwd());
    }

    /**
     * 设置消息密码
     */
    public void setMsgPwd(String pwd) {
        String md = TextUtils.getMD5(pwd);
        if (localAccountBean == null) {
            localAccountBean = NimUserHandler.getInstance().getLocalAccount();
        }
        localAccountBean.setMsgPwd(md);
        NimUserHandler.getInstance().syncChange2Service();
    }

    /**
     * 设置是否开启密语
     */
    public void setSecretMsg(boolean open) {
        if (localAccountBean == null) {
            localAccountBean = NimUserHandler.getInstance().getLocalAccount();
        }
        localAccountBean.setOpenReadDestroy(open);
        NimUserHandler.getInstance().syncChange2Service();
    }

    /**
     * 获取是否开启密语
     */
    public boolean getSecretMsg() {
        localAccountBean = NimUserHandler.getInstance().getLocalAccount();
        return localAccountBean.getOpenReadDestroy();
    }

    /**
     * 获取设置的密码
     */
    private String getMsgPwd() {
        localAccountBean = NimUserHandler.getInstance().getLocalAccount();
        return localAccountBean.getMsgPwd();
    }
}
